import { Component, Input, OnInit } from '@angular/core';
import { BannerService } from '@core/routes-services/banner.service';
import { Router } from '@angular/router';
import { MyModalHelper } from '@core/my-modal.helper';
import { NzMessageService } from 'ng-zorro-antd';

@Component({
  selector: 'banner-list',
  templateUrl: './list.component.html',
  styles: [`
    :host ::ng-deep .ant-card-body{
      padding: 0 24px;
    }
  `]
})
export class ListComponent implements OnInit {
  isLoading = false;
  keyword: any;
  @Input() type: any = 1;
  page: any = { number: 1, size: 10, totalElements: 0, content: [] };
  constructor(private message: NzMessageService,
              private router: Router,
              private bannerService: BannerService,
              private modalHelper: MyModalHelper) {
  }

  ngOnInit() {
    this.initPage();
  }


  initPage() {
    this.isLoading = true;
    this.bannerService.pagination(this.page, this.keyword,this.type).then((res) => {
      if (res.status.toString().startsWith('20')) {
        this.page = res.data;
        ++this.page.number;
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });
  }

  /**
   * 搜索重置分页数据
   */
  search() {
    this.page = { number: 0, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  changePageNo(pageNo) {
    this.page = { number: pageNo, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }
  //
  // editEntity = (entity) => {
  //   let temp: boolean;
  //   temp = myValidNull(entity.id);
  //   const bak = {};
  //   Object.assign(bak, entity);
  //   this.modalHelper.open(BannerEditComponent, { entity: bak,type:this.type }, 500, {
  //     nzTitle: '轮播图信息',
  //   }).subscribe(result => {
  //     if(result){
  //       temp ? this.search() : this.changePageNo(this.page.number);
  //     }
  //   }, (error) => {
  //     console.error('弹窗回调出错：', error);
  //   });
  // };

  addEntity = (data) => {
    this.router.navigate(['/banner/list/edit'], { queryParams: { id: 0 ,type: this.type} });
  };

  delete = (data) => {
    this.bannerService.deleteEntity(data.id).then((data) => {
      this.search();
      this.message.success('删除成功');
    }, error => console.log('删除出错，后续处理'));
  };


}
